﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Sublimation  
{
    public class Slot :GameObject
    {
        #region attributes
        protected bool building;
        public bool Building
        {
            get { return building; }
            set { building = value; }
        }
        protected float buildingTime;
        protected Texture2D rangeDisplay;
        protected Color rangeDisplayColor;
        protected Tower ownerTower;
        protected SlotKind kind;
        public SlotKind Kind
        {
            get{return kind;}
            set { kind = value; }
        }
        protected int production_cost;
        protected float attackDelay;
        public float AttackDelay
        {
            get
            {
                return attackDelay;
            }
            set
            {
                attackDelay = value;
            }
        }
        public int Production_cost
        {
            get
            {
                return production_cost;
            }
            set
            {
                production_cost = value;
            }
        }
        protected float orientation;
        public float Orientation
        {
            get
            {
                return orientation;
            }
            set
            {
                orientation = value;
            }
        }
        protected float angle;
        public float Angle
        {
            get
            {
                return angle;
            }
            set
            {
                angle = value;
            }
        }
        protected float damage;
        public float Damage
        {
            get { return damage; }
            set { damage = value; }
        }
        protected float range;
        public float Range
        {
            get
            {
                return range;
            }
            set
            {
                range = value;
            }
        }
        protected float rangeAffected;
        protected float damageAffected;
        protected float attackDelayAffected;

        #endregion


        public Slot(WorldProperties worldProperties, SlotKind kind, Tower ownerTower, bool built)
            : base(worldProperties)
        {
            LoadContent();
            this.ownerTower = ownerTower;
            this.worldProperties = worldProperties;
            rangeDisplayColor = Color.White;
            this.kind = kind;
            
            // make sure it loads and draws
            Scale = 1.0f;
            DrawOrder = 51;
            UpdateOrder = 50;
            Visible = true;
            Enabled = !ownerTower.Building;

            // align to parent
            Origin = Vector2.Zero;
            //this.Scale = 1.0f / SPRITE_SCALE_TO_ONE;
            //ScaleWithinObject(parent, ENERGY_SCALE_TO_PARENT);
            this.Velocity = Vector2.Zero;
            CentreOnObject(ownerTower);

            this.Building = !built;
            buildingTime = 0;
        }


        public float completion()
        {
            if (production_cost == 0)
            {
                return 1;
            }else{
                float total = production_cost * 1000;

                return (buildingTime / total);
            }
        }

        protected override void LoadContent()
        {
            base.LoadContent();
            Sprite = Game.Content.Load<Texture2D>("empty");
            rangeDisplay = Game.Content.Load<Texture2D>("Sprites\\range");
        }

        public override void Update(GameTime gameTime)
        {
            if (Building)
            {
                // mirror the parents movements
                buildingTime += gameTime.ElapsedGameTime.Milliseconds;
                if (buildingTime > production_cost * 1000)
                {
                    Building = false;
                    build();
                }
            }
            else
            {
                base.Update(gameTime);

                rangeAffected = range * ownerTower.RangedFactor;
                damageAffected = damage * ownerTower.DamageFactor;
                attackDelayAffected = attackDelay / ownerTower.RateFactor;
            }
            CentreOnObject(ownerTower);
            AlignOriginWithObject(ownerTower);
        }

        public virtual void build()
        {
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
            if (ownerTower.Mouving && !Building)
            {
                spriteBatch.Begin();
                spriteBatch.Draw(rangeDisplay, new Rectangle((int)ownerTower.Position.X, (int)ownerTower.Position.Y, (int)range, (int)(2 * range * angle / MathHelper.PiOver4)), null, rangeDisplayColor, ownerTower.Rotation + Orientation, new Vector2(0, 500), SpriteEffects.None, 1);
                spriteBatch.End();
            }
        }

        public float RealAngle()
        {
            return Orientation + Rotation;
        }
    }
}
